﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static MaterialDesignThemes.Wpf.Theme.ToolBar;

namespace TestMeterialDesign
{
    /// <summary>
    /// TaskExpanderBak.xaml 的交互逻辑
    /// </summary>
    public partial class TaskExpanderBak : UserControl
    {

        #region DependencyProperty
        /// <summary>
        /// 任务是否完成
        /// </summary>
        public bool IsFinished
        {
            get { return (bool)GetValue(IsFinishedProperty); }
            set { SetValue(IsFinishedProperty, value); }
        }

        public static readonly DependencyProperty IsFinishedProperty =
            DependencyProperty.Register("IsFinished", typeof(bool), typeof(TaskExpanderBak), new PropertyMetadata(false, OnFinishStateChange));

        public static void OnFinishStateChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //TaskExpanderBak taskExpander = (TaskExpanderBak)d;
            Debug.Print("OnFinishStateChange," + e.NewValue);
        }
        /// <summary>
        /// 任务是否重要
        /// </summary>
        public bool IsImportant
        {
            get { return (bool)GetValue(IsImportantProperty); }
            set { SetValue(IsImportantProperty, value); }
        }

        public static readonly DependencyProperty IsImportantProperty =
            DependencyProperty.Register("IsImportant", typeof(bool), typeof(TaskExpanderBak), new PropertyMetadata(false, OnImportantStateChange));

        public static void OnImportantStateChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //TaskExpanderBak taskExpander = (TaskExpanderBak)d;
            Debug.Print("OnImportantStateChange," + e.NewValue);
        }

        /// <summary>
        /// 任务是否能够弹出扩展
        /// </summary>
        public bool CanExpand
        {
            get { return (bool)GetValue(CanExpandProperty); }
            set { SetValue(CanExpandProperty, value); }
        }

        public static readonly DependencyProperty CanExpandProperty =
            DependencyProperty.Register("CanExpand", typeof(bool), typeof(TaskExpanderBak), new PropertyMetadata(false, OnExpandStateChange));

        private static void OnExpandStateChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TaskExpanderBak taskExpander = (TaskExpanderBak)d;
            bool state = (bool)e.NewValue;
            Debug.Print("OnExpandStateChange," + e.NewValue);
            if (state)
            {
                taskExpander.ExpandButtonVisibility = Visibility.Visible;
            }
            else
            {
                taskExpander.ExpandButtonVisibility = Visibility.Hidden;
            }
        }

        /// <summary>
        /// 任务是否能够弹出扩展
        /// </summary>
        public Visibility ExpandButtonVisibility
        {
            get { return (Visibility)GetValue(ExpandButtonVisibilityProperty); }
            set { SetValue(ExpandButtonVisibilityProperty, value); }
        }

        public static readonly DependencyProperty ExpandButtonVisibilityProperty =
            DependencyProperty.Register("ExpandButtonVisibility", typeof(Visibility), typeof(TaskExpanderBak), new PropertyMetadata(Visibility.Hidden, OnExpandButtonVisibilityStateChange));

        private static void OnExpandButtonVisibilityStateChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Debug.Print("OnExpandButtonVisibilityStateChange," + e.NewValue);
        }

        public ObservableCollection<SubTask> Items
        {
            get { return (ObservableCollection<SubTask>)GetValue(ItemsProperty); }
            set { SetValue(ItemsProperty, value); }
        }

        public static readonly DependencyProperty ItemsProperty =
            DependencyProperty.Register("Items", typeof(ObservableCollection<SubTask>), typeof(TaskExpanderBak), new PropertyMetadata(null, OnItemsChage));

        private static void OnItemsChage(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TaskExpanderBak taskExpander = (TaskExpanderBak)d;
            Debug.Print("OnItemsChage");

            if (taskExpander.Items != null && taskExpander.Items.Count > 0)
                taskExpander.CanExpand = true;
        }


        #endregion
        public TaskExpanderBak()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            Items =
            [
                new SubTask(){ID=1, Name="任务1" ,IsImportant=false ,IsFinished=false},
                new SubTask(){ID=2, Name="任务2" ,IsImportant=true  ,IsFinished=true},
                new SubTask(){ID=3, Name="任务3" ,IsImportant=false ,IsFinished=false}
            ];
        }

        private bool isSubTaskUnfinish = false;
        /// <summary>
        /// 子任务完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SubTask_Finished(object sender, RoutedEventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            int id = (int)cb.Tag;
            Debug.Print($"SubTask_Finished,{Items[id - 1]}");
            Debug.Print(Items[id - 1].IsFinished.ToString());
            IsFinished = CheckTaskFinish();
        }

        /// <summary>
        /// 子任务取消完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SubTask_Unfinished(object sender, RoutedEventArgs e)
        {
            isSubTaskUnfinish = true;
            CheckBox cb = (CheckBox)sender;
            int id = (int)cb.Tag;
            Debug.Print($"SubTask_Unfinished,{Items[id - 1]}");
            IsFinished = false;
        }

        /// <summary>
        /// 检测全部子任务是否完毕
        /// </summary>
        /// <returns></returns>
        private bool CheckTaskFinish()
        {
            foreach (var item in Items)
            {
                if (!item.IsFinished)
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 任务完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Task_Finished(object sender, RoutedEventArgs e)
        {
            Debug.Print("Task_Finished");
            foreach (var item in Items)
            {
                if (!item.IsFinished)
                {
                    item.IsFinished = true;
                }
            }
            Items = new ObservableCollection<SubTask>(Items);
            printcb();
        }
        private void printcb()
        {
            foreach (var item in Items)
            {
                Debug.Print(item.ToString());
            }

        }
        /// <summary>
        /// 任务取消完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Task_Unfinished(object sender, RoutedEventArgs e)
        {
            Debug.Print("Task_Unfinished");
            Debug.Print($"Task_Unfinished,sender type={sender.GetType().ToString()}");
            if (isSubTaskUnfinish)
            {
                isSubTaskUnfinish = false;
                return;
            }
            foreach (var item in Items)
            {
                if (item.IsFinished)
                {
                    item.IsFinished = false;
                }
            }
            Items = new ObservableCollection<SubTask>(Items);
            printcb();
        }
    }
}
